<?php

class CsvController extends Controller {

    public function actionJobreport() {
   
 header( 'Content-Type: text/csv' );
  header( 'Content-Disposition: attachment;filename=export.csv' );
        $statusList = JobsStatus::getListData();
        $criteria = new CDbCriteria();
        $criteria->order = "jobid ASC";
     
        $jobs = Jobs::model()->findAll($criteria);
        $result = 'JobNo;JobStatus;CreateDate;CustNo;LocNo;LocationName;Quote;QuoteAmount;SalesOrder;SalesOrderAmount;Invoice;InvoiceAmount;Quote1;QuoteAmount1;Quote2;QuoteAmount2;SalesOrder1;SalesOrderAmount1;SalesOrder2;SalesOrderAmount2;Invoice1;InvoiceAmount1;Invoice2;InvoiceAmount2'. "\n";
        /* @var $job Jobs */
        foreach ($jobs as $job) {
            $row = $job->jobid . ';'; 
            if(isset($statusList[$job->jobstatus]) )
                $row.=$statusList[$job->jobstatus];
            
            
             $row .=';'.$job->addeddate .';'. $job->custno . ';' . $job->locno . ';' .$job->getLocation()->locname . ';';
            if (isset($job->quoteList['0'])) {
                $inv = $job->quoteList['0'];
                $row.= $inv->invoice . ';';
               $row.= $inv->invoiceamount . ';';
            }else
                $row .=';;';
            if (isset($job->salesOrderList['0'])) {
                $inv = $job->salesOrderList['0'];
                $row.= $inv->invoice . ';';
                $row.= $inv->invoiceamount . ';';
            }else
                $row .=';;';
             if (isset($job->invoiceList['0'])) {
                $inv = $job->invoiceList['0'];
                $row.= $inv->invoice . ';';
                $row.= $inv->invoiceamount. ';';
            }else
                $row .=';;';
            if (isset($job->quoteList['1'])) {
                $inv = $job->quoteList['1'];
                $row.= $inv->invoice . ';';
                $row.= $inv->invoiceamount . ';';
            }else
                $row .=';;';
                if (isset($job->quoteList['2'])) {
                $inv = $job->quoteList['2'];
                $row.= $inv->invoice . ';';
                $row.= $inv->invoiceamount . ';';
            }else
                $row .=';;';
              if (isset($job->salesOrderList['1'])) {
                $inv = $job->salesOrderList['1'];
                $row.= $inv->invoice . ';';
                $row.= $inv->invoiceamount . ';';
            }else
                $row .=';;';
              if (isset($job->salesOrderList['2'])) {
                $inv = $job->salesOrderList['2'];
                $row.= $inv->invoice . ';';
                $row.= $inv->invoiceamount . ';';
            }else
                $row .=';;';
                  if (isset($job->salesOrderList['1'])) {
                $inv = $job->salesOrderList['1'];
                $row.= $inv->invoice . ';';
                $row.= $inv->invoiceamount . ';';
            }else
                $row .=';;';
                  if (isset($job->salesOrderList['2'])) {
                $inv = $job->salesOrderList['2'];
                $row.= $inv->invoice . ';';
                $row.= $inv->invoiceamount. "\n";
            }else
                $row .=';;'."\n";
            $result .=$row;
          
        }
          echo $result;
    }

    // Uncomment the following methods and override them if needed
    /*
      public function filters()
      {
      // return the filter configuration for this controller, e.g.:
      return array(
      'inlineFilterName',
      array(
      'class'=>'path.to.FilterClass',
      'propertyName'=>'propertyValue',
      ),
      );
      }

      public function actions()
      {
      // return external action classes, e.g.:
      return array(
      'action1'=>'path.to.ActionClass',
      'action2'=>array(
      'class'=>'path.to.AnotherActionClass',
      'propertyName'=>'propertyValue',
      ),
      );
      }
     */
}